package com.turnengine.client.global.admin.command.set;

import static com.javabi.common.dependency.DependencyFactory.getDependency;

import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.errorcode.ErrorCodeException;
import com.javabi.command.executor.ICommandExecutorService;
import com.turnengine.client.global.admin.command.ExecuteScript;
import com.turnengine.client.global.admin.command.IExecuteScript;
import com.turnengine.client.global.admin.command.IPing;
import com.turnengine.client.global.admin.command.ISendEmail;
import com.turnengine.client.global.admin.command.Ping;
import com.turnengine.client.global.admin.command.SendEmail;

/**
 * The Global Admin Command Set.
 */
public class GlobalAdminCommandSet implements IGlobalAdminCommandSet {

	@Override
	public boolean ping() {
		IPing command = new Ping();
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public boolean sendEmail(long loginId, String sender, String recipient, String subject, String body) {
		ISendEmail command = new SendEmail(loginId, sender, recipient, subject, body);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public boolean executeScript(long loginId, String name) {
		IExecuteScript command = new ExecuteScript(loginId, name);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}
}
